import { HttpClient } from '@angular/common/http';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ArticleInfoData, InformationService } from 'src/app/service/information.service';
import { environment } from 'src/environments/environment';

declare let PDFLib: any;

@Component({
  selector: 'app-information-detail',
  templateUrl: './information-detail.component.html',
  styleUrls: ['./information-detail.component.scss']
})
export class InformationDetailComponent implements OnInit, OnDestroy {

  constructor(
    private route: ActivatedRoute,
    private information: InformationService,
    private router: Router,
    private http: HttpClient
  ) { }

  ngOnInit(): void {
    this.route.queryParams.subscribe({
      next: (res: any) => {
        this.getInformationDetail(res.id)
      }
    })
  }

  informationDetail: any
  getInformationDetail(id: string){
    this.information._getContentDetail(id).subscribe({
      next: (value: ArticleInfoData) => {
        if(value.content){
          value.content = value.content.replace(/<video/g,'<video autoplay="true" ')
					value.content = value.content.replace(/<table/g,'<table border="1" cellspacing="0"')
        }
        this.informationDetail = value;
        // (document.querySelector('#information-content') as HTMLDivElement).innerHTML = value.content as string
      },
      error: (err) => {
          
      },
    })
  }

  dealContent(detail: any){
    return detail
  }

  ngOnDestroy(): void {
  }


  async getPdf() {
    const url = 'assets/pdf/用户生活给水系统设计、施工及验收规范.pdf'

    const existingPdfBytes = await this.http.get(url, { responseType: 'arraybuffer' }).toPromise()
    this.modifyPdf(existingPdfBytes)
  }
  async modifyPdf(existingPdfBytes: any) {

    const pdfDoc = await PDFLib.PDFDocument.load(existingPdfBytes)
    
    const pdfBytes = await pdfDoc.save()
    // const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true });
    // 预览
    const pdfBlobData = new Blob([pdfBytes], { type: "application/pdf;Base64" });
    setTimeout(() => {
      (document.getElementById('pdf-preview') as HTMLIFrameElement).src = window.URL.createObjectURL(pdfBlobData);
    }, 2000)
  }

}
